package com.drive.controller;

import com.drive.bean.Subject;
import com.drive.constant.MessageConstant;
import com.drive.entity.PageResult;
import com.drive.entity.QueryPageBean;
import com.drive.entity.Result;
import com.drive.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 往
 * 科目管理
 */
@RestController
@RequestMapping("/subject")
public class SubjectController {
    @Autowired
    private SubjectService subjectService;

    //新增
    @RequestMapping("/add")
    public Result add(@RequestBody Subject subject, Integer[] coachIds){
        try{
            subjectService.add(subject,coachIds);
        }catch (Exception e){
            return new Result(false, MessageConstant.ADD_SUBJECTL_FAIL);
        }
        return new Result(true,MessageConstant.ADD_SUBJECTL_SUCCESS);
    }
    //分页查询
    @RequestMapping("/findPage")
    public PageResult queryPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = subjectService.findPage(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString()
        );
        return pageResult;
    }

    //根据id查询
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            Subject subject = subjectService.findById(id);
            return new Result(true,MessageConstant.QUERY_SUBJECTL_SUCCESS,subject);
        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_SUBJECTL_FAIL);
        }
    }

    //根据id查询科目教练关系表
    @RequestMapping("/findSubjectAndcoachIds")
    public Result findSubjectAndcoachIds(Integer id){
        try{
            List<Integer> coachIds = subjectService.findSubjectAndCoachIds(id);
            return new Result(true,MessageConstant.QUERY_COASH_SUCCESS,coachIds);
        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_COASH_FAIL);
        }
    }

    //更新
    @RequestMapping("/update")
    public Result updata(@RequestBody Subject subject,Integer[] coachIds){
        try{
            subjectService.update(subject,coachIds);
            return new Result(true,MessageConstant.EDIT_SUBJECT_SUCCESS);
        }catch (Exception e){
            return new Result(false,MessageConstant.EDIT_SUBJECT_FAIL);
        }
    }

    //根据id删除
    @RequestMapping("/deleteById")
    public Result delete(Integer id){
        try{
            subjectService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_SUBJECT_SUCCESS);
        }catch (Exception e){
            return new Result(false,MessageConstant.DELETE_SUBJECT_FAIL);
        }
    }
}
